package org.lgiavedoni.android.franq.service.phone.location.updatelocation.imp;

import org.lgiavedoni.android.franq.core.envent.Event;
import org.lgiavedoni.android.franq.core.envent.listener.EventListener;
import org.lgiavedoni.android.franq.core.position.PositionManager;
import org.lgiavedoni.android.franq.event.PhoneMove;
import org.lgiavedoni.android.franq.service.event.EventService;
import org.lgiavedoni.android.franq.service.phone.location.updatelocation.UpdateLocationStrategy;

public class UpdateLocationByMovementStrategy implements UpdateLocationStrategy {

	private final MyEventListener eventListener = new MyEventListener();

	public void applayStrategy() {
		EventService.inst.addObserver(eventListener);
	}

	public void cancelStrategy() {
		EventService.inst.removeObserver(eventListener);
	}

	private static class MyEventListener extends EventListener {

		@Override
		public void exec(final Event event) {
			PositionManager.inst.tryToUpdatePosition();
			/*
			 * // Check the current status if (thread != null &&
			 * thread.isAlive()) return;
			 * 
			 * thread = new Thread() {
			 * 
			 * @Override public void run() { try {
			 * setName("UpdateLocationByMovementStrategy"); // TODO add time
			 * restriction. No update before 10 mins // the phone is moving
			 * PositionManager.inst.tryToUpdatePosition(); // Wait x mins and
			 * update the location
			 * ThreadUtil.sleep(DateUtil.getMillisecondsOfMins(5));
			 * PositionManager.inst.tryToUpdatePosition(); // last after the
			 * disable time of sensor long sleepTime =
			 * (event.getCreationDate().getTime() +
			 * ConfigService.inst.getValueAsLong
			 * (ConfigKey.SENSOR_LISTENER_DISABLE_PERIOD) + 60000) -
			 * DateUtil.getTimeInMillis(); if (sleepTime > 0) {
			 * ThreadUtil.sleep(sleepTime);
			 * PositionManager.inst.tryToUpdatePosition(); } } catch (Exception
			 * ex) { Logger.inst.error(ex); } } }; thread.start();
			 */
		}

		@Override
		public Class getClassEvent() {
			return PhoneMove.class;
		}
	}
}
